package com.easylinkin.linkappapi.quality.controller;

import cn.hutool.core.codec.Base64;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.quality.dto.QualityQuestionInfoDto;
import com.easylinkin.linkappapi.quality.dto.QualityQuestionStatisticsDTO;
import com.easylinkin.linkappapi.quality.service.QualityInspectionAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * @author: kan yuanfeng
 * @Date: 2020/11/04 11:42
 * @Description: 质量检查台账
 */
@RestController
@RequestMapping("qualityInspectionAccount")
@Api(tags = "质量检查台账")
public class QualityInspectionAccountController {
  @Autowired
  private QualityInspectionAccountService qualityInspectionAccountService;

  /**
   * @Description: 查询质量台账列表
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping("list")
  @ApiOperation("根据条件，分页(不分页)查询")
  public RestMessage queryListByPage(@RequestBody RequestModel<QualityQuestionInfoDto> requestModel){
    Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
    Assert.notNull(requestModel.getPage(), "page 不能为空");
    IPage<QualityQuestionInfoDto> record =  qualityInspectionAccountService.queryListByPage(requestModel);
    return RestBuilders.successBuilder().data(record).build();
  }

  /**
   * @Description: 查询数量
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping("count")
  @ApiOperation("查询数量")
  public RestMessage getCount(@RequestBody QualityQuestionInfoDto questionInfoDto){
    Map<String, String> map = qualityInspectionAccountService.getCount(questionInfoDto,null);
    return RestBuilders.successBuilder().data(map).build();
  }

  /**
   * @Description: 质量台账详细
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("{id}")
  @ApiOperation("质量台账详细")
  public RestMessage findById(@PathVariable("id")Integer id,Integer type){
    Assert.notNull(id, "id不能为空");
    Assert.notNull(type, "type不能为空");
    QualityQuestionInfoDto qualityQuestionInfoDto =  qualityInspectionAccountService.findById(id,type);
    return RestBuilders.successBuilder().data(qualityQuestionInfoDto).build();
  }

  /**
   * @Description: 台账单条导出
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("export/{id}")
  @ApiOperation("台账单条导出")
  public void exportById(@RequestHeader("User-Agent") String header,@PathVariable("id")Integer id,Integer type,
      HttpServletResponse response)
      throws IOException {
    //获取并组织excel数据
    Workbook workbook = qualityInspectionAccountService.exportById(id,type);

    String filename;
    // 当前日期，用于导出文件名称
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    String s = "质量台账-"+sdf.format(new Date());
    if(header.contains("Firefox")){
      //说明是火狐浏览器,使用Base64Encoder类进行编码
      filename = "=?utf-8?B?"+ Base64.encode(s.getBytes("utf-8"))+"?=";
    }else{
      //是其他浏览器,使用URLEncoder类进行编码
      filename = String.valueOf(URLEncoder.encode(s, "UTF-8"));
    }
    // 指定下载的文件名--设置响应头
    response.setHeader("Content-Disposition", "attachment;filename=" +filename+".xlsx");
    response.setContentType("application/vnd.ms-excel;charset=UTF-8");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    // 写出数据输出流到页面
    OutputStream output = response.getOutputStream();
    BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
    workbook.write(bufferedOutPut);
    bufferedOutPut.flush();
    bufferedOutPut.close();
    output.close();
  }

  /**
   * @Description: 质量台账统计
   * @author kan yuanfeng
   * @date 2023/05/26 11:42
   */
  @PostMapping("statistics")
  @ApiOperation("质量台账统计")
  public RestMessage getStatistics(@RequestBody QualityQuestionInfoDto qualityQuestionInfoDto){
    QualityQuestionStatisticsDTO questionStatisticsDTO = qualityInspectionAccountService.getStatistics(qualityQuestionInfoDto);
    return RestBuilders.successBuilder().data(questionStatisticsDTO).build();
  }

  /**
   * @Description: 删除质量台账
   * @author kan yuanfeng
   * @date 2023/05/26 11:42
   */
  @DeleteMapping
  @ApiOperation("删除质量台账")
  public RestMessage delBatch(@RequestBody List<QualityQuestionInfoDto> list){
    qualityInspectionAccountService.delBatch(list);
    return RestBuilders.successBuilder().build();
  }
}
